<?php
namespace app\admin\my;

class MyPage extends My {
	private $_table = [];
	private $_keyList = [];
	private $_buttonList = [];
	private $_pagination = [];
	private $_data = [];
	private $_db = '';

	private $_statusUrl = "setStatus"; // 状态修改连接
	private $_deleteTrueUrl = "delete"; // 彻底删除连接

	private $_search = [];
	private $_select = [];
	private $_form = [];
	
	// -- 状态修改链接
	public function setDb($db) {
		$this->_db = $db;
		return $this;
	}
	public function title($title) {
		$this->_table['title'] = $title;
		return $this;
	}
	public function suggest($suggest) {
		$this->_table['suggest'] = $suggest;
		return $this;
	}
	public function tips($content) { // --未使用
		$this->_table['tips'] = $content;
		return $this;
	}
	public function url($url='') {
		$this->_table['url'] = $url;
		return $this;
	}
	public function height($length=1000) {
		$this->_table['height'] = $length;
		return $this;
	}
	public function showHeader($flag=false) {
		$this->_table['show_header'] = $flag;
		return $this;
	}
	public function showFooter($flag=true) {
		$this->_table['show_footer'] = $flag;
		return $this;
	}
	// --- 按钮
	public function buttonsAlign($align='left') { // 按钮布局
		$this->_table['buttons_align'] = $align;
		return $this;
	}
	public function showRefresh($flag=false) { // 显示 刷新 按钮
		$this->_table['show_refresh'] = $flag;
		return $this;
	}
	public function showMap($flag=true) { // 显示 过滤 按钮
		$this->_table['show_filter'] = $flag;
		return $this;
	}
	public function showToggle($flag=false) { // 显示 数据方式切换 按钮
		$this->_table['show_toggle'] = $flag;
		return $this;
	}
	public function showColumns($flag=false) { // 显示 字段过滤 按钮
		$this->_table['show_columns'] = $flag;
		return $this;
	}
	public function showPaginationSwitch($flag=false) { // 显示 分页 按钮
		$this->_table['show_pagination_switch'] = $flag;
		return $this;
	}
	public function pagination($flag=false) {
		$this->_table['pagination'] = $flag;
		return $this;
	}
	public function paginationLoop($flag=false) { //--有问题
		$this->_table['pagination_loop'] = $flag;
		return $this;
	}
	public function pageNumber($number=1) {
		$this->_table['page_number'] = $number;
		return $this;
	}
	public function pageSize($size=20) {
		$this->_table['page_size'] = $size;
		return $this;
	}
	public function pageList($arr_list='[10,20,50,100,200]') {
		$this->_table['page_list'] = $arr_list;
		return $this;
	}
	public function onlyInfoPagination($flag=true) {
		$this->_table['only_info_pagination'] = $flag;
		return $this;
	}
	public function sidePagination($side='client') { // -未测试
		$this->_table['side_pagination'] = $side;
		return $this;
	}
	public function paginationHAlign($align='left') {
		$this->_table['side_pagination'] = $side;
		return $this;
	}
	public function paginationVAlign($align='top') { // bottom top both
		$this->_table['side_pagination'] = $side;
		return $this;
	}
	public function paginationDetailHAlign($align='left') {
		$this->_table['pagination_detail_h_align'] = $side;
		return $this;
	}
	public function paginationPreText($text='&lsaquo;') {
		$this->_table['pagination_pre_text'] = $text;
		return $this;
	}
	public function paginationNextText($text='&rsaquo;') {
		$this->_table['pagination_next_text'] = $text;
		return $this;
	}
	public function striped($flag=false) {
		$this->_table['striped'] = $flag;
		return $this;
	}
	public function search($flag=false) {
		$this->_table['search'] = $flag;
		return $this;
	}
	public function searchAlign($align='left') {
		$this->_table['search_align'] = $align;
		return $this;
	}
	public function searchOnEnterKey($flag=false) { // --有问题
		$this->_table['search_on_enter_key'] = $flag;
		return $this;
	}
	public function strictSearch($flag=true) { // --
		$this->_table['search_on_enter_key'] = $flag;
		return $this;
	}
	public function searchText($text=true) { // --
		$this->_table['search_text'] = $text;
		return $this;
	}
	public function cardView($flag=true) { // --
		$this->_table['card_view'] = $flag;
		return $this;
	}
	public function detailView($flag=true) { // --
		$this->_table['detail_view'] = $flag;
		return $this;
	}
	
	public function selectItemName() {
		$this->_table['select_item_name'] = $flag;
		return $this;
	}

	// -- 状态修改链接
	public function setStatusUrl($url) {
		if ( !$url ) $url="setStatus";
		$this->_statusUrl = $url;
		dump($this->_statusUrl );
		return $this;
	}
	// -- 彻底删除链接
	public function setDeleteTrueUrl($url) {
		if ( $url ) $url = "delete";
		$this->_deleteTrueUrl = $url;
		return $this;
	}

	// -- 按钮
	public function button($title, $attr) {
		$item = array(
			'title' => $title,
			'attr' => $attr,
			'url' => $attr['url'],
			'icon' => $attr['icon'],
		);

		if ( isset($attr['target']) ) $item['target'] = $attr['target'];
		if ( isset($attr['confirm']) ) $item['confirm'] = $attr['confirm'];
		if ( isset($attr['ajax']) ) $item['ajax'] = $attr['ajax'];
		
		$this->_buttonList[] = $item;

		return $this;
	}
	// -- 新增按钮
	public function buttonNew($url='', $param=[], $title='新增', $attr=[]) {
		if ( $url=='' ) { // 默认链接
			$action = request()->action();
			$url = $action."Edit";
		}

		$attr['url'] = url($url, $param);
		$attr['icon'] = 'fa fa-plus';
		$attr['target'] = '_blank';
		return $this->button($title, $attr);
	}
	// -- 编辑按钮 - now no use
	public function buttonEdit($url='', $param=[], $title='编辑', $attr=[]) {
		if ( $url=='' ) { // 默认链接
			$action = request()->action();
			$url = $action."Edit";
		}

		$attr['url'] = url($url, $param);
		$attr['icon'] = 'glyphicon-edit icon-pencil-square-o';
		$attr['target'] = '_blank';
		return $this->button($title, $attr);
	}

	// -- ajax按钮 attr中设置（'hide-data' => 'true'）表示，不必须要勾选对象，即不必须ids有值
	public function ajaxButton($url, $params, $title, $attr=[]) {
		$attr['class'] = 'btn ajax-post btn-default';
		$attr['url'] = $this->addUrlParam($url, $params);
		$attr['target-form'] = 'ids';
		return $this->button($title, $attr);
	}
	// -- 模态弹窗按钮
	public function buttonModal($url, $params, $title, $attr=[]) {
		// $attr data-title 模态框标题 target-form 要传输的数据
		$attr['modal-url'] = $this->addUrlParam($url, $params);
		$attr['data-role'] = 'modal_popup';
		return $this->button($title, $attr);
	}

	// -- 状态设置按钮
	public function buttonSetStatus($table, $url, $status, $title, $attr) {
		if ( $url ) {
		} else if ( $this->_statusUrl ) {
			$url = $this->_statusUrl;
		} else {
			$url = "setStatus";
		}

		// $attr['class'] = isset($attr['class']) ? $attr['class'] : 'btn ajax-post btn-default';
		$attr['url'] = $this->addUrlParam($url, array(
			'table' => $table,
			'status' => $status,
		));
		$attr['target-form'] = 'ids';
		return $this->button($title, $attr);
	}
	// -- 禁用按钮 0
	public function buttonDisable($table, $url=null, $title='禁用', $attr=[]) {
		// $attr['class'] = 'btn ajax-post btn-warning';
		$attr['icon'] = 'glyphicon-remove icon-check-square';
		$attr['ajax'] = true;
		return $this->buttonSetStatus($table, $url, 0, $title, $attr);
	}
	// -- 启用按钮 1
	public function buttonEnable($table, $url=null, $title='启用', $attr=[]) {
		// $attr['class'] = 'btn ajax-post btn-info';
		$attr['icon'] = 'glyphicon-ok icon-check-square-o';
		$attr['ajax'] = true;
		return $this->buttonSetStatus($table, $url, 1, $title, $attr);
	}
	// -- 软删除按钮 -1
	public function buttonTrash($table, $url=null, $title='删除', $attr=[]) {
		// $attr['class'] = 'btn ajax-post btn-danger';
		$attr['icon'] = 'glyphicon-trash icon-trash-o';
		$attr['confirm'] = true;
		$attr['ajax'] = true;
		return $this->buttonSetStatus($table, $url, -1, $title, $attr);
	}
	// -- 还原按钮 1
	public function buttonRestore($table, $url=null, $title='还原', $attr=[]) {
		// $attr['class'] = 'btn ajax-post btn-info';
		$attr['icon'] = 'glyphicon-share-alt icon-check-square-o';
		$attr['ajax'] = true;
		return $this->buttonSetStatus($table, $url, 1, $title, $attr);
	}

	// 清空回收站按钮 - 删除软删除全部
	public function buttonClear($model=null) {
		return $this->button('清空回收站', array(
			'class' => 'btn ajax-post btn-default tox-confirm',
			'data-confirm' => '确定清空回收站？',
			'url' => url('', array('model' => $model)),
			'target-form' => 'ids',
			'hide-data' => 'true'
		));
	}
	// 彻底删除按钮
	public function buttonDelete($url=null) {
		if ( !$url ) $url = $this->_deleteTrueUrl;
		$attr['class'] = 'btn ajax-post btn-default tox-confirm';
		$attr['data-confirm'] = '确定删除所有？';
		$attr['url'] = $url;
		$attr['target-form'] = 'ids';
		return $this->button('删除所有', $attr);
	}

	// 排序按钮
	public function buttonSort($href, $title='排序', $attr=[]) {
		$attr['href'] = $href;
		return $this->button($title, $attr);
	}
	// 筛选
	public function select($title='筛选', $name='key', $type='select', $des='', $attr, $arrdb='', $arrvalue=null) {
		if ( empty($arrdb) ) $this->_select[] = array('title' => $title, 'name' => $name, 'type' => $type, 'des' => $des, 'attr' => $attr, 'arrvalue' => $arrvalue);
		return $this;
	}
	// 
	public function selectPlateForm($id, $method, $action) {
	   $this->_form[] = array('id'=>$id, 'method'=>$method, 'action'=>$action);
		return $this;
	}

	// -- 字段
		public function key($name, $title, $type, $opt=null, $width='150px') {
			$this->_keyList[] = array('name' => $name, 'title' => $title, 'type' => $type, 'opt' => $opt, 'width' => $width);
			return $this;
		}
		public function keyText($name, $title, $opt=null) {
			return $this->key($name, text($title), 'text', $opt);
		}
		public function keyTruncText($name, $title, $length) {
			return $this->key($name, $title, 'trunktext', $length);
		}
		public function keyId($name='id', $title='ID') {
			return $this->keyText($name, $title);
		}
		public function keyTitle($name='title', $title='标题') {
			return $this->keyText($name, $title);
		}

	// -- html
	public function keyHtml($name, $title, $width='150px') {
		return $this->key($name, op_h($title), 'html', null, $width);
	}
	// -- 链接
	public function keyLink($name, $title, $getUrl, $target="_self") {
		if ( is_string($getUrl) ) $getUrl = $this->createDefaultGetUrlFunction($getUrl);
		// if ( empty($name) ) $name = $title;
		return $this->key($name, $title, 'link', array('url' => $getUrl, 'target' => $target));
	}
	// -- 带替换表示的链接
	public function keyLinkByFlag($name, $title, $getUrl, $flag='id') {
		if ( is_string($getUrl) ) $getUrl = $this->ParseUrl($getUrl, $flag);
		return $this->key($name, $title, 'link', $getUrl);
	}
	// -- 图标
		public function keyIcon($name='icon', $title='图标') {
			return $this->key($name, $title, 'icon');
		}
		public function keyImage($name, $title) {
			return $this->key($name, $title, 'image');
		}
	// -- 映射
		public function keyMap($name, $title, $map) {
			return $this->key($name, $title, 'map', $map);
		}
		public function keyYesNo($name, $title) {
			$map = [
				0 => '否',
				1 => '是'
			];
			return $this->keymap($name, $title, $map);
		}
		public function keyBool($name, $title) {
			return $this->keyYesNo($name, $title);
		}
		public function keyStatus($name='status', $title='状态') {
			$map = [
				-1 => '删除',
				0 => '禁用',
				1 => '启用'
			];
			return $this->key($name, $title, 'status', $map);
		}
	// -- 时间
		public function keyTime($name, $title) {
			return $this->key($name, $title, 'time');
		}
		public function keyCTime($name='create_time', $title='创建时间') {
			return $this->keyTime($name, $title);
		}
		public function keyMTime($name='update_time', $title='更新时间') {
			return $this->keyTime($name, $title);
		}

	// - 用户标识
		public function keyUid($name='uid', $title='用户') {
			return $this->key($name, $title, 'uid');
		}
		public function keyNickname($name='uid', $title, $subtitle=null) {
			return $this->key($name, $title, $subtitle, 'nickname');
		}

	// -- 关联表字段显示+URL连接
	public function keyJoin($name, $title, $mate, $return, $model, $url='') {

		$map = array('name' => $name,'mate' => $mate, 'return' => $return, 'model' => $model, 'url' => $url);
		return $this->key($name, $title, 'Join', $map);
	}
	// -- 操作
		public function action($getUrl, $text, $title='操作', $attr=[]) {
			if ( is_string($getUrl) ) $getUrl = $this->createDefaultGetUrlFunction($getUrl);

			$doActionKey = null;
			foreach ( $this->_keyList as $key ) {
				if ( $key['name'] === 'DOACTIONS' ) {
					$doActionKey = $key;
					break;
				}
			}
			if ( !$doActionKey ) $this->key('DOACTIONS', $title, 'doaction', []);

			$doActionKey = null;
			foreach ( $this->_keyList as &$key ) {
				if ( $key['name'] == 'DOACTIONS' ) {
					$doActionKey = &$key;
					break;
				}
			}

			$doActionKey['opt']['actions'][] = array('text' => $text, 'get_url' => $getUrl, 'opt' => $attr);
			return $this;
		}
		public function actionEdit($url='', $text='编辑') {
			if ( $url=='' ) {
				$action = request()->action();
				$url = $action."Edit?id=###";
			}
			return $this->action($url, $text);
		}
		public function actionStatus() { // -- 设置状态
			$status = [
				'enable' => ['number' => '1', 'name' => '启用', 'show' => '0'], // 启用
				'disable' => ['number' => '0', 'name' => '禁用', 'show' => '1'], // 禁用
				'delete' => ['number' => '-1', 'name' => '删除', 'show' => '0,1,2'], // 删除
				'revert' => ['number' => '1', 'name' => '还原', 'show' => '-1'], // 还原
				'verify' => ['number' => '1', 'name' => '审核', 'show' => '2'], // 审核
			];
			$that = $this;

			foreach ( $status as $value ) {
				$getUrl = function ($item) use ($that, $status, $value) {
					$value['show'] = explode(',', $value['show']);
					if ( in_array($item['status'], $value['show']) ) {
						$pattern = str_replace('###', $item['id'], $that->_statusUrl."?id=###"); // id替换
						return $that->addUrlParam($pattern, ['status' => $value['number'], 'table'=>$that->_db]);
					} else {
						return '';
					}
				};
				$this->action($getUrl, $value['name'], array('class' => 'ajax-get'));
			}

			return $this;
		}
		public function actionDelete($title="真删除") { // -- 硬删除
			$that = $this;

			$getUrl = function ($item) use ($that) {
				$pattern = str_replace('###', $item['id'], $that->_deleteTrueUrl."?id=###"); // id替换
				return  $that->addUrlParam($pattern, array('table'=>$that->_db));
			};

			return $this->action($getUrl, $title, array('class' => 'ajax-get'));
		}
		public function actionModal($getUrl, $text, $width=1000, $height=600, $modal_title='', $title='操作', $attr=[]) {
			$attr['data-role'] = 'modal_popup';
			$attr['data-width'] = $width;
			$attr['data-height'] = $height;
			$attr['data-title'] = $modal_title;
			return $this->action($getUrl, $text, $title, $attr);
		}
	// --- 过滤
		public function map($name, $title, $type='text', $options=[], $extend=null) {
			switch ( $type ) {
				case 'select':
					return $this->mapSelect($name, $title, $options, $extend);
				break;
				case 'select2':
					return $this->mapSelect2($name, $title, $options, $extend);
				break;
				default:
					return $this->mapText($name, $title, $extend);
			}
		}
		public function mapText($name, $title, $extend=null) {
			$this->_search[] = array(
				'name' => $name,
				'title' => $title,
				'type' => 'text',
				'extend' => $extend
			);

			return $this;
		}
		public function mapSelect($name, $title, $options=[]) {
			$this->_search[] = array(
				'name' => $name,
				'title' => $title,
				'type' => 'select',
				'options' => $options
			);

			return $this;
		}
		public function mapSelect2($name, $title, $options=[], $extend=null) {
			$this->_search[] = array(
				'name' => $name,
				'title' => $title,
				'type' => 'select2',
				'options' => $options,
				'extend' => $extend
			);

			return $this;
		}
		public function mapIn($name, $title, $options=[]) {
			return $this->mapSelect2($name, $title, $options, array('in' => true));
		}
		public function mapLike($name, $title, $extend=null) {
			return $this->mapText($name, $title, array('like' => true));
		}
		public function mapStatus($name='status', $title='状态', $options=[
			'-1'=>'已删除',
			'0'=>'禁用',
			'1'=>'启用'
		]) {
			return $this->mapIn($name, $title, $options);
		}
		
		public function mapTime($name="time", $title="时间", $type='time') { // 时间搜索
			$this->_search[] = array(
				'name' => $name,
				'title' => $title,
				'type' => $type,
			);
		}
		public function mapDate($name="date", $title="日期") { // 日期搜索
			$this->mapTime($name, $title, 'date');
		}
		public function mapDateTime($name="datetime", $title="日期时间") { // 日期时间搜索
			$this->mapTime($name, $title, 'datetime');
		}
		public function mapCTime($name="create_time", $title="创建时间") { // 创建时间搜索
			$this->mapDateTime($name, $title);
		}
		public function mapMTime($name="update_time", $title="编辑时间") { // 编辑时间搜索
			$this->mapDateTime($name, $title);
		}
	// -- 数据
		private function convertKey($from, $to, $convertFunction) {
			foreach ( $this->_keyList as &$key ) {
				if ( $key['type'] == $from ) {
					$key['type'] = $to;

					foreach ( $this->_data as &$data ) {
						$value = &$data[$key['name']];
						$value = $convertFunction($value, $key, $data);
						unset($value);
					}
					unset($data);
				}
			}
			unset($key);
		}
		public function data($data) {
			$this->_data = $data;
			$this->convertKey('map', 'text', function ($value, $key) { // 映射
				return $key['opt'][$value];  //todo:: 这个地方会报错，未索引的数组 ,偶尔出现，应该与map冲突有关，keymap
			});
			$this->convertKey('time', 'text', function ($value) { // 时间
				if ( $value != 0 ) {
					return time_format($value);
				} else {
					return '-';
				}
			});
			$this->convertKey('trunktext', 'text', function ($value, $key) { // trunktext
				$length = $key['opt'];
				return msubstr($value, 0, $length);
			});

			$this->convertKey('text', 'html', function ($value) { // text
				return $value;
			});
			$this->convertKey('link', 'html', function ($value, $key, $item) { // link
				$value = htmlspecialchars($value);
				$getUrl = $key['opt']['url'];
				$url = $getUrl($item);

				return "<a href=\"$url\" target=\"" . $key['opt']['target'] . "\" class=\"J_menuItem\">$value</a>";
			});

			$this->convertKey('icon', 'html', function ($value, $key, $item) { // icon
				$value = htmlspecialchars($value);
				if ( $value == '' ) {
					$html = '[空]';
				} else {
					$html = "<i class=\"$value\"></i> $value";
				}
				return $html;
			});
			$this->convertKey('image', 'html', function ($value, $key, $item) { // image
				$value = htmlspecialchars($value);
				if ( intval($value) ) { // value是图片标识 - todo behero
					// $sc_src = get_cover($value, 'path');

					// $src = getThumbImageById($value, 80, 80);
					// $sc_src = $sc_src=='' ? $src : $sc_src;
				} else { // value是图片路径
					$sc_src = $value;
				}
				$html = "<div class='popup-gallery'><a title=\"查看大图\" href=\"$sc_src\"><img src=\"$sc_src\"/ style=\"height: 40px\"></a></div>";
				return $html;
			});
			$this->convertKey('doaction', 'html', function ($value, $key, $item) { // doaction
				$actions = $key['opt']['actions'];
				$result = [];
				foreach ( $actions as $action ) {
					$getUrl = $action['get_url'];
					$linkText = $action['text'];
					$url = $getUrl($item);

					if ( !empty($url) ) {
						if ( isset($action['opt']) ) {
							$content = [];
							foreach ( $action['opt'] as $key => $value ) {
								$value = htmlspecialchars($value);
								$content[] = "$key=\"$value\"";
							}
							$content = implode(' ', $content);
							if ( isset($action['opt']['data-role']) && $action['opt']['data-role'] == "modal_popup" ) { // 模态弹窗
								$result[] = "<a href=\"javascript: void(0);\" modal-url=\"$url\" " . $content . ">$linkText</a>";
							} else {
								$result[] = "<a href=\"$url\" " . $content . ">$linkText</a>";
							}
						} else {
							$result[] = "<a href=\"$url\">$linkText</a>";
						}
					}
				}
				return implode(' ', $result);
			});
			$this->convertKey('Join', 'html', function ($value, $key) { // Join
				if ( $value != '' ) {
					$val = get_table_field($value, $key['opt']['mate'], $key['opt']['return'], $key['opt']['model']);

					if ( !$key['opt']['url'] ) {
						return $val;
					} else {
						$urld = url($key['opt']['url'], array($key['opt']['name'] => $value));
						return "<a href=\"$urld\" target=\"_blank\">$val</a>";
					}
				} else {
					return '-';
				}
			});

			$statusUrl = $this->_statusUrl;
			$that = &$this;
			
			$this->convertKey('status', 'html', function ($value, $key, $item) use ($statusUrl, $that) { // 状态
				$map = $key['opt'];
				$text = $map[$value];
				
				if ( !$statusUrl ) return $text;

				$switchStatus = $value == 1 ? 0 : 1;
				$url = $that->addUrlParam($statusUrl, array('status' => $switchStatus, 'id' => $item['id'], 'table' => $that->_db));
				
				return "<a href=\"{$url}\" class=\"ajax-get\">$text</a>";
			});

			$this->convertKey('html', 'html', function ($value) { // 如果html为空
				if ( $value === '' ) return '<span style="color: #bbb;">【空】</span>';
				if ( $value === NULL ) return '<span style="color: #bbb;">【NULL】</span>';
				return $value;
			});

			return $this->_data;
		}
		public function fetch($template=false, $vars=[], $replace=[], $config=[]) {
			foreach ( $this->_buttonList as &$button ) { // 编译buttonList中的属性
				$button['tag'] = isset($button['attr']['href']) ? 'a' : 'button';
				$this->addDefaultCssClass($button);
				$button['attr'] = $this->compileHtmlAttr($button['attr']);
			}

			$this->assign('buttonList', $this->_buttonList); // 按钮
			$this->assign('keyList', $this->_keyList); // 字段

			$this->assign('table', $this->_table); // 表格信息
			$this->assign('searches', $this->_search); // 字段信息

			return parent::fetch("page");
		}

	public function doSetStatus($model, $ids, $status=1) { // 设置状态
		$id = array_unique((array)$ids);
		$rs = M($model)->where(array('id' => array('in', $id)))->save(array('status' => $status));
		if ( $rs === false ) {
			$this->error('设置失败');
		} else {
			$this->success('设置成功', $_SERVER['HTTP_REFERER']);
		}
	}
	// -- 添加默认样式 - 按钮
	private function addDefaultCssClass(&$button) {
		if ( !isset($button['attr']['class']) ) {
			$button['attr']['class'] = 'btn btn-default';
		} else { // isset($button['attr']['class']
			$button['attr']['class'] .= ' btn btn-default';
		}
	}
	// -- 生成链接 id=### other_id={$other_id}
	private function createDefaultGetUrlFunction($pattern) {
		$explode = explode('|', $pattern);
		$pattern = $explode[0];
		$fun = empty($explode[1]) ? 'url' : $explode[1];

		return function ($item) use ($pattern, $fun) {
			$pattern = str_replace('###', $item['id'], $pattern); // id替换

			$view = new \think\View(); // 其他变量替换
			$view->assign($item);

			$pattern = $view->display($pattern);

			return $fun($pattern);
		};
	}
	// -- 附加查询参数
	public function addUrlParam($url, $params) {
		if ( strpos($url, '?') === false ) {
			$seperator = '?';
		} else {
			$seperator = '&';
		}

		$params = http_build_query($params);
		return $url . $seperator . $params;
	}
	// 自动处理清空回收站
	public function clearTrash($model='') {
		if ( IS_POST ) {
			if ( $model != '' ) {
				$aIds = I('post.ids', array());
				if ( !empty($aIds) ) {
					$map['id'] = array('in', $aIds);
				} else {
					$map['status'] = -1;
				}

				$result = model($model)->where($map)->delete();
				if ( $result ) $this->success('_SUCCESS_TRASH_CLEARED_');
				$this->error('_TRASH_ALREADY_EMPTY_');
			} else {
				$this->error('_TRASH_SELECT_');
			}
		}
	}
	// 执行彻底删除数据，只适用于无关联的数据表
	public function doDeleteTrue($model, $ids) {
		$ids = is_array($ids) ? $ids : explode(',', $ids);
		model($model)->where(array('id' => array('in', $ids)))->delete();
		$this->success('_SUCCESS_DELETE_COMPLETELY_', $_SERVER['HTTP_REFERER']);
	}
	// -- 解析Url - 链接 标识
	private function ParseUrl($pattern, $flag) {
		return function ($item) use ($pattern, $flag) { // 返回闭包函数
			$pattern = str_replace('###', $item[$flag], $pattern); // id替换

			$view = new \think\View(); // 其他变量替换
			$view->assign($item);
			$pattern = $view->display($pattern);

			return url($pattern);
		};
	}
}